package com.tan.config;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: longshifu
 * @time: 2022/8/3 21:04
 */
@Configuration
public class DirectRabbitConfig {
    @Autowired
    RabbitAdmin rabbitAdmin;

    @Bean
    public RabbitAdmin rabbitAdmin(CachingConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        rabbitAdmin.setIgnoreDeclarationExceptions(true);
        return rabbitAdmin;
    }

    @Bean
    public void createQueueExchange() {
        rabbitAdmin.declareQueue(new Queue("blog.queue", true, false, false));
        rabbitAdmin.declareExchange(new DirectExchange("blog.exchange", true, false));
        rabbitAdmin.declareBinding(BindingBuilder
                .bind(new Queue("blog.queue", true, false, false))
                .to(new DirectExchange("blog.exchange", true, false))
                .with("blogAddOrUpdate"));

    }

}
